package com.liuzhilong.infrastructure.framework.spring.processor;

import com.liuzhilong.infrastructure.framework.spring.component.TestBeanPostProcessorComponet;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class TestBeanPostProcessor implements BeanPostProcessor {

    //

    /**
     * 初始化之前处理 <br>
     * <p>
     *     此处进行简单的赋值操作，在实际操作中吗，可以利用反射操作进行动态处理
     * </p>
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof TestBeanPostProcessorComponet){
            System.out.println(TestBeanPostProcessor.class.getName()+"我在bean初始化之前执行");
            TestBeanPostProcessorComponet componet = (TestBeanPostProcessorComponet)bean;
            System.out.println(TestBeanPostProcessor.class.getName()+" 设置TestBeanPostProcessorComponet name");

            componet.setName("postProcessBeforeInitialization");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof TestBeanPostProcessorComponet){
            System.out.println(TestBeanPostProcessor.class.getName()+"我在bean初始化之后执行");
        }
        return bean;
    }
}
